/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.core;

import java.lang.reflect.Modifier;
import java.util.List;
import org.nop.core.Application;
import org.nop.core.PackageProcessor;

/**
 *
 * @author Alexey Andreev
 */
public class CorePackageProcessor implements PackageProcessor {
    @Override
    public void processPackage(Application app, String packageName, List<Class<?>> classes) {
        MenuFeature menu = app.get(MenuFeature.class);
        BlocksFeature blocks = app.get(BlocksFeature.class);
        FragmentsFeature fragments = app.get(FragmentsFeature.class);
        for (Class<?> cls : classes) {
            if (MenuItemRenderer.class.isAssignableFrom(cls) &&
                    (cls.getModifiers() & Modifier.ABSTRACT) == 0 && !cls.isInterface()) {
                menu.registerItem(cls.asSubclass(MenuItemRenderer.class));
            }
            if (BlockRenderer.class.isAssignableFrom(cls) &&
                    (cls.getModifiers() & Modifier.ABSTRACT) == 0 && !cls.isInterface()) {
                blocks.registerBlock(cls.asSubclass(BlockRenderer.class));
            }
            if (FragmentRenderer.class.isAssignableFrom(cls) && !cls.isInterface()) {
                for (Class<?> iface : cls.getInterfaces()) {
                    if (FragmentRenderer.class.isAssignableFrom(iface)) {
                        fragments.registerFragment(iface.asSubclass(FragmentRenderer.class),
                                cls.asSubclass(FragmentRenderer.class));
                    }
                }
            }
        }
    }
}
